<template>
	<view class="container">
		<text class="title">商品信息</text>
		
		<view class="product">
			<image :src="pic ? pic : '/static/icon/product_default.png'" class="productLeft" />
			<view class="productRight">
				<text class="productTitle two-line-title">{{title}}</text>
				<counter :quantity="quantity" :limit="limit" @minus="onCounterMinus" @plus="onCounterPlus" />
			</view>
		</view>
		
		<view class="btnWrap">
			<view class="cancelBtn" @click="close">
				<text class="btnText">取消</text>
			</view>
			<view class="addBtn" @click="onOk">
				<text class="btnText">加入购物车</text>
			</view>
		</view>

	</view>
</template>

<script>
	/**
	 * @property {Boolean} beforeClose 是否拦截取消事件
	 * @event {Function} confirm 点击确认按钮触发
	 * @event {Function} close 点击取消按钮触发
	 */
	import counter from '@/components/counter/counter.vue'

	export default {
		name: "uniPopupProductDialog",
		components: {counter},
		props: {
			/**
			 * 拦截取消事件 ，如果拦截取消事件，必须监听close事件，执行 done()
			 */
			beforeClose: {
				type: Boolean,
				default: false
			},
			title: {
				type: String,
				default: '产品名称'
			},
			pic: {
				type: String,
				default: '/static/icon/product_default.png'
			},
			limit: {
				type: Number,
				default: 10
			}
		},
		data() {
			return {
				focus: false,
				quantity: 1
			}
		},
		inject: ['popup'],
		created() {
			// 对话框遮罩不可点击
			this.popup.mkclick = false
		},
		methods: {
			/**
			 * 点击确认按钮
			 */
			onOk() {
				this.$emit('confirm', () => {
					this.popup.close()
				}, this.quantity)
			},
			/**
			 * 点击取消按钮
			 */
			close() {
				if (this.beforeClose) {
					this.$emit('close', () => {
						this.popup.close()
					})
					return
				}
				this.popup.close()
			},
			onCounterMinus() {
				console.log('onCounterMinus...')
				this.quantity--
			},
			onCounterPlus() {
				console.log('onCounterPlus...')
				this.quantity++
			}
		}
	}
</script>

<style lang="scss">
	.container {
		/* #ifndef APP-NVUE */ 
		display: flex;
		/* #endif */ 
		flex-direction: column;
		background-color: #ffffff;
		width: 500rpx;
		height: 380rpx;
		border-radius: 20rpx;
		padding: 40rpx 35rpx 35rpx 40rpx;
	}
	.title {
		font-size: 32rpx;
		text-align: center;
		margin-bottom: 20rpx;
	}
	.product {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		padding: 20rpx 0;
	}
	.productLeft {
		width: 120rpx;
		height: 120rpx;
	}
	.productRight {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: flex-start;
		justify-content: space-between;
		flex: 1;
		// background-color: pink;
		padding-left: 20rpx;
	}
	.productTitle {
		font-size: 30rpx;
	}
	.btnWrap {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		width: 420rpx;
		margin-top: 20rpx;
	}
	.cancelBtn {
		background-color: #404040;
		padding: 20rpx 50rpx;
		border-radius: 40rpx;
	}
	.addBtn {
		background-color: #f43f26;
		padding: 20rpx 30rpx;
		border-radius: 40rpx;
	}
	.btnText {
		font-size: 32rpx;
		color: #ffffff;
		text-align: center;
	}
</style>
